<!-- C:\Users\王硕\Desktop\毕设\housing-rental-system-frontend\新建文件夹\vue-demo\src\views\Setting.vue -->
<template>
  <div class="setting-container">
    <!-- 页面标题 -->
    <h2 class="page-title">系统设置</h2>

    <!-- 基础设置模块 -->
    <el-card class="setting-card">
      <h3 class="section-title">⚙️ 基础设置</h3>
      <el-form label-width="120px">
        <el-form-item label="系统名称">
          <el-input
            v-model="systemConfig.name"
            placeholder="请输入系统名称"
            clearable
          />
        </el-form-item>

        <el-form-item label="系统Logo">
          <el-avatar
            :src="systemConfig.logo"
            :size="100"
            shape="square"
            class="logo-preview"
          />
        </el-form-item>

        <el-form-item label="主题颜色">
          <el-color-picker v-model="systemConfig.themeColor" show-alpha />
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 通知设置模块 -->
    <el-card class="setting-card">
      <h3 class="section-title">📨 通知设置</h3>
      <el-form label-width="120px">
        <el-form-item label="邮件通知">
          <el-switch v-model="notifyConfig.email" />
          <span class="tip-text">（订单状态变更、系统通知）</span>
        </el-form-item>

        <el-form-item label="短信通知">
          <el-switch v-model="notifyConfig.sms" />
          <span class="tip-text">（验证码、紧急通知）</span>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据管理模块 -->
    <el-card class="setting-card">
      <h3 class="section-title">💾 数据管理</h3>
      <div class="data-actions">
        <el-button type="warning" @click="handleBackup" :icon="Download">
          立即备份
        </el-button>

        <el-button type="danger" @click="handleClean" :icon="Delete">
          清理数据
        </el-button>
      </div>

      <el-divider />

      <el-timeline class="backup-list">
        <el-timeline-item
          v-for="(item, index) in backups"
          :key="index"
          :timestamp="item.time"
          placement="top"
        >
          <el-card shadow="never">
            <div class="backup-item">
              <span class="backup-name">{{ item.name }}</span>
              <el-tag type="info" size="small">{{ item.size }}</el-tag>
            </div>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-card>

    <!-- 操作日志模块 -->
    <el-card class="setting-card">
      <h3 class="section-title">📜 操作日志</h3>
      <el-table :data="operationLogs" stripe height="300" style="width: 100%">
        <el-table-column prop="time" label="时间" width="160" />
        <el-table-column prop="operator" label="操作人" width="120">
          <template #default="{ row }">
            <el-tag :type="getUserTagType(row.operator)" effect="plain">
              {{ row.operator }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="action" label="操作内容" />
        <el-table-column prop="ip" label="IP地址" width="140" />
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Download, Delete } from "@element-plus/icons-vue";

// 系统基础配置
const systemConfig = reactive({
  name: "房屋租赁管理系统",
  logo: "https://cdn.example.com/logo.png",
  themeColor: "#409EFF",
});

// 通知配置
const notifyConfig = reactive({
  email: true,
  sms: false,
});

// 备份数据
const backups = reactive([
  {
    time: "2024-03-20 14:00:00",
    name: "full_backup_20240320.zip",
    size: "2.3GB",
  },
  {
    time: "2024-03-19 09:00:00",
    name: "incremental_backup_20240319.zip",
    size: "356MB",
  },
]);

// 操作日志
const operationLogs = reactive([
  {
    time: "2024-03-21 10:15:00",
    operator: "admin",
    action: "修改系统主题颜色为 #67C23A",
    ip: "192.168.1.100",
  },
  {
    time: "2024-03-20 14:30:00",
    operator: "manager",
    action: "执行数据备份操作",
    ip: "192.168.1.101",
  },
]);

// 处理备份操作
const handleBackup = () => {
  const newBackup = {
    time: new Date().toLocaleString(),
    name: `backup_${new Date().toISOString().slice(0, 10)}.zip`,
    size: `${(Math.random() * 3).toFixed(1)}GB`,
  };
  backups.unshift(newBackup);
  ElMessage.success("备份操作已完成");
};

// 处理数据清理
const handleClean = () => {
  ElMessageBox.confirm("此操作将删除所有测试数据，是否继续？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      ElMessage.success("模拟数据清理完成");
    })
    .catch(() => {
      ElMessage.info("已取消操作");
    });
};

// 获取用户标签样式
const getUserTagType = (user) => {
  const userTypes = {
    admin: "danger",
    manager: "warning",
    user: "success",
  };
  return userTypes[user] || "info";
};
</script>

<style scoped>
.setting-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-title {
  color: #333;
  margin-bottom: 30px;
}

.setting-card {
  margin-bottom: 24px;
  transition: box-shadow 0.3s;
}

.setting-card:hover {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.section-title {
  color: #333;
  margin-bottom: 20px;
  font-size: 18px;
  display: flex;
  align-items: center;
}

.section-title::before {
  content: "";
  display: inline-block;
  width: 4px;
  height: 16px;
  background: #409eff;
  margin-right: 8px;
}

.logo-preview {
  border: 1px dashed #ddd;
  cursor: pointer;
  transition: border-color 0.3s;
}

.logo-preview:hover {
  border-color: #409eff;
}

.data-actions {
  margin-bottom: 20px;
  display: flex;
  gap: 12px;
}

.backup-list {
  margin-top: 20px;
}

.backup-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.backup-name {
  flex: 1;
  margin-right: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tip-text {
  color: #999;
  font-size: 12px;
  margin-left: 8px;
}
</style>
